package com.easychat;

import com.easychat.websocket.netty.NettyWebSocketStarter;
import org.apache.ibatis.session.SqlSessionException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import javax.sql.DataSource;

/**
 * @program: easychat-java
 * @description:
 * @author: Mr.Wang
 * @create: 2024-12-07 14:15
 **/

@Component("initRun")
public class InitRun implements ApplicationRunner {

    private static final Logger logger = LoggerFactory.getLogger(InitRun.class);

    @Resource
    DataSource dataSource;

    @Resource
    private NettyWebSocketStarter nettyWebSocketStarter;

    @Override
    public void run(ApplicationArguments args) throws Exception {
        try{
            dataSource.getConnection();
            new Thread(nettyWebSocketStarter).start();
            logger.info("服务启动成功 perfectServerStartup");
        }catch (SqlSessionException e){
            logger.error("数据库连接失败",e);
        }
        catch (Exception e){
            logger.error("服务启动失败",e);
        }
    }
}
